using System;
using System.Linq;
using Microsoft.Win32;

namespace WIT.DAQ.Core.Helper;

internal class RegistryHelper
{
	internal static bool Exists(RegistryKey parentKey, string subKey, string valueName)
	{
		bool result = false;
		RegistryKey registryKey = parentKey.OpenSubKey(subKey);
		if (registryKey != null)
		{
			try
			{
				result = registryKey.GetValueNames().Contains(valueName);
			}
			catch
			{
				result = false;
			}
			finally
			{
				registryKey.Close();
			}
		}
		return result;
	}

	internal static bool ReadValue(RegistryKey parentKey, string subKey, string valueName, out object value)
	{
		bool result = false;
		value = null;
		RegistryKey registryKey = parentKey.OpenSubKey(subKey);
		if (registryKey != null)
		{
			try
			{
				result = registryKey.GetValueNames().Contains(valueName);
				value = registryKey.GetValue(valueName);
			}
			catch
			{
				result = false;
			}
			finally
			{
				registryKey.Close();
			}
		}
		return result;
	}

	internal static bool WriteValue(RegistryKey parentKey, string subKey, string valueName, object value, RegistryValueKind registryValueKind = RegistryValueKind.String)
	{
		string errMsg;
		return WriteValue(parentKey, subKey, valueName, value, out errMsg, registryValueKind);
	}

	internal static bool WriteValue(RegistryKey parentKey, string subKey, string valueName, object value, out string errMsg, RegistryValueKind registryValueKind = RegistryValueKind.String)
	{
		bool result = false;
		errMsg = string.Empty;
		RegistryKey registryKey = parentKey.CreateSubKey(subKey);
		try
		{
			registryKey.SetValue(valueName, value, registryValueKind);
			result = true;
		}
		catch (Exception ex)
		{
			result = false;
			errMsg = ex.Message;
		}
		finally
		{
			registryKey.Close();
		}
		return result;
	}

	internal static bool DeleteValue(RegistryKey parentKey, string subKey, string valueName, bool throwOnMissingValue = false)
	{
		string errMsg;
		return DeleteValue(parentKey, subKey, valueName, out errMsg, throwOnMissingValue);
	}

	internal static bool DeleteValue(RegistryKey parentKey, string subKey, string valueName, out string errMsg, bool throwOnMissingValue = false)
	{
		bool result = false;
		errMsg = string.Empty;
		RegistryKey registryKey = parentKey.OpenSubKey(subKey, writable: true);
		if (registryKey != null)
		{
			try
			{
				registryKey.DeleteValue(valueName, throwOnMissingValue);
				result = true;
			}
			catch (Exception ex)
			{
				result = false;
				errMsg = ex.Message;
			}
			finally
			{
				registryKey.Close();
			}
		}
		return result;
	}

	internal static bool UpdateValue(RegistryKey parentKey, string subKey, string valueName, object value, RegistryValueKind registryValueKind = RegistryValueKind.String)
	{
		string errMsg;
		return UpdateValue(parentKey, subKey, valueName, value, out errMsg, registryValueKind);
	}

	internal static bool UpdateValue(RegistryKey parentKey, string subKey, string valueName, object value, out string errMsg, RegistryValueKind registryValueKind = RegistryValueKind.String)
	{
		bool result = false;
		errMsg = string.Empty;
		RegistryKey registryKey = parentKey.OpenSubKey(subKey, writable: true);
		if (registryKey != null)
		{
			try
			{
				registryKey.SetValue(valueName, value, registryValueKind);
				result = true;
			}
			catch (Exception ex)
			{
				result = false;
				errMsg = ex.Message;
			}
			finally
			{
				registryKey.Close();
			}
		}
		return result;
	}
}
